package com.rongji.dfish.hello.index.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

import com.rongji.dfish.base.Page;
import com.rongji.dfish.base.Utils;
import com.rongji.dfish.framework.FilterParam;
import com.rongji.dfish.hello.entity.PubUser;
import com.rongji.dfish.hello.pub.view.CommonView;
import com.rongji.dfish.ui.command.AjaxCommand;
import com.rongji.dfish.ui.command.ConfirmCommand;
import com.rongji.dfish.ui.command.DialogCommand;
import com.rongji.dfish.ui.command.SubmitCommand;
import com.rongji.dfish.ui.form.Radiogroup;
import com.rongji.dfish.ui.form.Text;
import com.rongji.dfish.ui.helper.FlexGrid;
import com.rongji.dfish.ui.helper.FormPanel;
import com.rongji.dfish.ui.helper.GridPanel;
import com.rongji.dfish.ui.layout.ButtonBar;
import com.rongji.dfish.ui.layout.View;
import com.rongji.dfish.ui.layout.grid.GridColumn;
import com.rongji.dfish.ui.layout.grid.Tr;
import com.rongji.dfish.ui.widget.Button;
import com.rongji.dfish.ui.widget.Html;
import com.rongji.dfish.ui.widget.PageBar;

import com.rongji.dfish.ui.command.JSCommand;

@Component
public class UserView extends CommonView {

	public View buildIndexView(List<PubUser> userList, Page page, FilterParam fp) {
		View view = buildMainView();

		Html mainTitle = (Html) view.findNodeById(W_MAIN_TITLE);
		mainTitle.setText("人员管理");

		ButtonBar mainOper = (ButtonBar) view.findNodeById(W_MAIN_OPER);
		mainOper.add(new Button("新增").setOn(Button.EVENT_CLICK, "this.cmd('edit')"));
		mainOper.add(new Button("删除").setOn(Button.EVENT_CLICK,
				"var userId=VM(this).fv('selectItem');if(!userId){$.alert('请选择要删除的记录');}else{this.cmd('delete',userId);}"));

		FlexGrid mainSearch = (FlexGrid) view.findNodeById(W_MAIN_SEARCH);
		mainSearch.addLabelRow(new Text("qUserName", "姓名", fp.getValueAsString("qUserName")), 4);
		mainSearch.addLabelRow(new Text("qLoginName", "登录号", fp.getValueAsString("qLoginName")), 4);
		mainSearch.addLabelRow(new Text("qMobilePhone", "手机号", fp.getValueAsString("qMobilePhone")), 4);

		ButtonBar btn = new ButtonBar(null).setAlign(ButtonBar.ALIGN_RIGHT).setSpace(10);
		btn.add(new Button("查询").setOn(Button.EVENT_CLICK, "this.cmd('search');"));
		btn.add(new Button("重置").setOn(Button.EVENT_CLICK, "VM(this).resetForm(this.closest('grid'));"));
		mainSearch.add(btn, -1);

		GridPanel mainContent = (GridPanel) view.findNodeById(W_MAIN_CONTENT);
		int dataColumnIndex = 0;
		mainContent.getPub().setOn(Tr.EVENT_DBLCLICK, "this.cmd('edit',$uId);");
		mainContent.addColumn(GridColumn.gridTriplebox("uId", "40"));
		mainContent.addColumn(GridColumn.hidden(dataColumnIndex++, "uId"));
		mainContent.addColumn(GridColumn.text(dataColumnIndex++, "uName", "姓名", "*"));
		mainContent.addColumn(GridColumn.text(dataColumnIndex++, "loginName", "登录号", "200"));
		mainContent.addColumn(GridColumn.text(dataColumnIndex++, "gender", "性别", "60"));
		mainContent.addColumn(GridColumn.text(dataColumnIndex++, "phone", "手机号", "80"));
		mainContent.addColumn(GridColumn.text(0, "oper", "操作", "80")
				.setFormat("<a href=javascript:; onclick=\"VM(this).cmd('edit','$uId')\">编辑</a>"
						+ "&nbsp;&nbsp;<a href=javascript:; onclick=\"VM(this).cmd('delete','$uId')\">删除</a>"));

		if (Utils.notEmpty(userList)) {
			List<Object[]> gridData = new ArrayList<Object[]>(userList.size());
			for (PubUser user : userList) {
				gridData.add(new Object[] { user.getUserId(), user.getUserName(), user.getLoginName(),
						Utils.isEmpty(user.getGender()) ? "未知" : ("2".equals(user.getGender()) ? "女" : "男"),
						user.getMobilePhone() });
			}
			mainContent.setGridData(gridData);
		}

		PageBar mainPage = (PageBar) view.findNodeById(W_MAIN_PAGE);
		mainPage.setPage(page);

		view.addCommand("edit", new DialogCommand("edit", "std", "信息编辑", DialogCommand.WIDTH_LARGE,
				DialogCommand.HEIGHT_LARGE, DialogCommand.POSITION_MIDDLE, "user/edit?userId=$0"));
		view.addCommand("search", new SubmitCommand("user/search"));
		view.addCommand("delete", new ConfirmCommand("确定删除所选记录?").setYes(new AjaxCommand("user/delete?userId=$0")));
		return view;
	}

	public FilterParam getParam(HttpServletRequest request) {
		FilterParam fp = new FilterParam();
		fp.registerKey("qUserName");
		fp.registerKey("qLoginName");
		fp.registerKey("qMobilePhone");

		fp.bindRequest(request);
		return fp;
	}

	public View buildEditView(PubUser user) {

		user = user == null ? new PubUser() : user;
		View view = buildDialogView();

		FormPanel main = (FormPanel) view.findNodeById(DLG_MAIN);
		main.addHidden("userId", user.getUserId());
		main.add(new Text("userName", "姓名", user.getUserName()).setRequired(true));
		main.add(new Text("loginName", "登录号", user.getLoginName()).setRequired(true));
		main.add(new Text("mobilePhone", "手机号", user.getMobilePhone()));
		main.add(new Radiogroup("gender", "性别", user.getGender(),
				Arrays.asList(new String[][] { { "1", "男" }, { "2", "女" } })).setRequired(true));

		ButtonBar btnBar = (ButtonBar) view.findNodeById(DLG_OPER);
		btnBar.add(new Button("保存").setOn(Button.EVENT_CLICK, "this.cmd('save')"));
		btnBar.add(new Button("关闭").setOn(Button.EVENT_CLICK, "$.close(this)"));

		view.addCommand("save", new SubmitCommand("user/save"));

		return view;
	}

}
